package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ChooseMenu {
    int chosenOption;
    int userRegisterIndex;
    boolean isUserRegistered;
    LoginDetails loginDetailsInstance;
    private String userOption = "Y";


    public void chooseMenuOption() throws IOException {
        do{
        System.out.println("Choose an option to proceed further");
        System.out.println("1.Login if you are already a registered member");
        System.out.println("2.Sign up if you want to register");
        System.out.println("3.Forgot password?Get a new one");
        System.out.println("4.View Membership Details");
        System.out.println("5.View all books in the library");
        System.out.println("6.View all movies in the library");

        chosenOption = Integer.parseInt(this.getInputFromUser());
        switch (chosenOption) {
            case 1:
            { this.loginIfAlreadyRegistered();
              break;}
            case 2:
            { this.signUpIfNewMember();
                break;}
            case 3:
            { this.forgotPassword();
                break;}
            case 4:
            {this.viewMembershipDetails();
                break;}
            case 5:
            {this.callPopulateBooks();
                break;}
            case 6:
            {this.callPopulateMovies();
                break;}
        }
        System.out.println("Do you want to explore more");
        userOption = this.getInputFromUser();
        }while (userOption.equals("Y"));
    }

    public String getInputFromUser() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String temp = br.readLine();
            return temp;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public void loginIfAlreadyRegistered() throws IOException {
        String userName ,password;
        loginDetailsInstance = new LoginDetails();
        loginDetailsInstance.populateAllUserDetails();
        System.out.println("Enter Your Login ID or UserName");
        userName = loginDetailsInstance.getLoginDetailsFromUser();
        System.out.println("Enter Your Password");
        password = loginDetailsInstance.getLoginDetailsFromUser();
        userRegisterIndex = loginDetailsInstance.searchUsername(userName);
        isUserRegistered = loginDetailsInstance.verifyPassword(password,userRegisterIndex);
        if (isUserRegistered)
        System.out.println("You are successfully logged in!");
        else
        System.out.println("You have not registered yet");

    }

    public void signUpIfNewMember() {
        System.out.println("Select a valid option");
    }

    public void forgotPassword() {
        System.out.println("Select a valid option");
    }

    public void viewMembershipDetails() {
        if (isUserRegistered){
        LoginDetails requiredLoginDetailsObject = this.loginDetailsInstance.getUserPasswordRepository().get(userRegisterIndex);
        System.out.println("NAME : "+requiredLoginDetailsObject.getName());
        System.out.println("EMAIL-ID : "+requiredLoginDetailsObject.getEmailID());
        System.out.println("ADDRESS : "+requiredLoginDetailsObject.getAddress());
        System.out.println("PHONE-NUMBER : "+requiredLoginDetailsObject.getPhoneNum());
        }
        else
        System.out.println("Please talk to the librarian.Thank You!");
    }

    public void callPopulateBooks() {
        PopulateBooks addBooks = this.getAddBooks();
        addBooks.addBooksToRepository();
    }

    public PopulateBooks getAddBooks() {
        return new PopulateBooks();
    }

    public void callPopulateMovies() {
        PopulateMovies addMovies = this.getAddMovies();
        addMovies.addMoviesToRepository();
    }

    public PopulateMovies getAddMovies() {
        return new PopulateMovies();
    }
}
